package org.academic.texteditor.gui.component;

import java.awt.Color;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JPanel;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.EditorKit;

import org.academic.texteditor.gui.component.menu.MenuFactory;
import org.academic.texteditor.gui.feature.Page;
import org.academic.texteditor.gui.helper.DocumentManager;
import org.academic.texteditor.gui.helper.Reference;
import org.academic.texteditor.gui.plugin.Plugin;

public class PageContainer extends JPanel {

	private static final long serialVersionUID = -4272842272725655981L;

	private final List<Page> pages;
	private final List<Plugin> plugins;
	private final List<Reference> references;

	public PageContainer() {
		this.pages = new ArrayList<Page>();
		this.plugins = new ArrayList<Plugin>();
		this.references = new ArrayList<Reference>();

		Reference ref1 = new Reference();
		ref1.setYear(2009);
		ref1.addAuthor("Author 1");
		references.add(ref1);

		Reference ref2 = new Reference();
		ref2.setYear(1987);
		ref2.addAuthor("Author 2");
		references.add(ref2);
		
		setLayout(new PageLayout());
		setBackground(Color.GRAY);
	}
	
	public Page getActualPage() {
		return pages.get(0);
	}
	
	public Page addPage(Page page) {
		pages.add(page);
		indexPages();
		
		super.add(page);
		
		return page;
	}
	
	public Page removePage(Page page) {
		pages.remove(page);
		indexPages();
		
		super.remove(page);
		
		return page;
	}
	
	public List<Page> getPages() {
		return pages;
	}

	public void addPlugin(Plugin plugin) {
		plugins.add(plugin);
	}

	public void removePlugin(Plugin plugin) {
		plugins.remove(plugin);
	}
	
	public List<Plugin> getPlugins() {
		return plugins;
	}

	public void addReference(Reference reference) {
		references.add(reference);
	}

	public void removeReference(Reference reference) {
		references.remove(reference);
	}
	
	public List<Reference> getReferences() {
		return references;
	}

	public void initialize() {
		for (Plugin plugin : plugins) {
			plugin.initialize(this);
		}
	}
	
	public void activate() {
		MenuFactory.getEdit().setVisible(true);
		
		DocumentManager.getInstance().setActualPageContainer(getName());
		
		for (Plugin plugin : plugins) {
			plugin.activate();
		}
	}

	public void deactivate() {
		MenuFactory.getEdit().setVisible(false);
		
		DocumentManager.getInstance().setActualPageContainer(null);
		
		for (Plugin plugin : plugins) {
			plugin.deactivate();
		}
	}
	
	private void indexPages() {
		for (int i = 0; i < pages.size(); i++) {
			Page page = pages.get(i);
			
			page.setIndex(i + 1);
		}
	}

	public void openDocument(FileReader reader) {
		Page firstPage = pages.get(0);
		PersonalizedTextPane textPane = firstPage.getText().getTextPane();
		EditorKit editorKit = textPane.getEditorKit();
		try {
			editorKit.read(reader, textPane.getDocument(), 0);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (BadLocationException e) {
			e.printStackTrace();
		}
	}

	public void saveDocument(FileWriter writer) {
		Page firstPage = pages.get(0);
		PersonalizedTextPane textPane = firstPage.getText().getTextPane();
		EditorKit editorKit = textPane.getEditorKit();
		try {
			Document doc = textPane.getDocument();
			editorKit.write(writer, doc, 0, doc.getLength());
		} catch (IOException e) {
			e.printStackTrace();
		} catch (BadLocationException e) {
			e.printStackTrace();
		}
	}
	
}
